package com.leoandroid.tool.toolsbox.largeread.activity;

import android.Manifest;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.leoandroid.tool.toolsbox.largeread.R;
import com.leoandroid.tool.toolsbox.largeread.fragment.MagnifierFragment;
import com.leoandroid.tool.toolsbox.largeread.fragment.MultiFunctionFragment;
import com.leoandroid.tool.toolsbox.largeread.fragment.MyFragment;

import java.util.ArrayList;
import java.util.List;

public class ToolsMainOldActivity extends BaseActivity {

    public static final String TAG = "ToolsMainActivity>>";
    List<String> mPermissionList = new ArrayList<>();//权限集合
    String[] permissions = new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE};
    private FragmentManager fragmentManager;
    private FragmentTransaction transaction;
    private MagnifierFragment fragMagnifier;
    private Toolbar top_tools;
    private Button top_button;
    private TextView top_text;
    List<Fragment> mFragments = new ArrayList<>();
    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            fragmentManager = getSupportFragmentManager();
            transaction = fragmentManager.beginTransaction();
            int itemId = item.getItemId();
            if (itemId == R.id.navigation_magnifier) {
                fragMagnifier = new MagnifierFragment();
                transaction.replace(R.id.content, fragMagnifier);
                top_text.setText(R.string.top_magnifier);
                top_button.setVisibility(View.GONE);
                transaction.commit();
                return true;
            } else if (itemId == R.id.navigation_classify) {//                    ClassifyFragment frag2 = new ClassifyFragment();
                //CalendarFragment frag2 = new CalendarFragment();
                transaction.replace(R.id.content, MultiFunctionFragment.getInstance());
                top_text.setText(R.string.top_calendar);
                top_button.setVisibility(View.GONE);
                transaction.commit();
                return true;
            } else if (itemId == R.id.navigation_my) {
                MyFragment frag3 = new MyFragment();
                transaction.replace(R.id.content, frag3);
                top_text.setText(R.string.top_my);
                top_button.setVisibility(View.GONE);
                transaction.commit();
                return true;
            }
            return false;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_tools_main);

        BottomNavigationView navigation = findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
        top_tools = (Toolbar) findViewById(R.id.toolbar);
        top_text = (TextView) findViewById(R.id.top_text);
        top_button = (Button) findViewById(R.id.top_button);
        setDefaultFragment();

//        //关于申请权限的代码
//        mPermissionList.clear();
//        for (int i = 0; i < permissions.length; i++) {
//            if (ContextCompat.checkSelfPermission(ToolsMainActivity.this, permissions[i]) != PackageManager.PERMISSION_GRANTED) {
//                mPermissionList.add(permissions[i]);
//            }
//        }
//        if (mPermissionList.isEmpty()) {//未授予的权限为空，表示都授予了
//            //Toast.makeText(MainActivity.this, "已经授权", Toast.LENGTH_LONG).show();
//        } else {//请求权限方法
//            boolean camere = SPUtil.with(this).load().read("CAMERA_DENIED",false);
//            boolean story = SPUtil.with(this).load().read("STORAGE_DENIED",false);
//            if(camere||story){
//                return;
//            }
//            String[] permissions = mPermissionList.toArray(new String[mPermissionList.size()]);//将List转为数组
//            ActivityCompat.requestPermissions(ToolsMainActivity.this, permissions, 2);
//        }       //申请结束
    }
    private void showFragment(Fragment fragment){
        hiddenFragments();
        if (mFragments == null) mFragments = new ArrayList<>();
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        if (mFragments.contains(fragment)) {
            fragmentTransaction.show(fragment);
        } else {
            fragmentTransaction.add(R.id.content, fragment);
            mFragments.add(fragment);
        }
        fragmentTransaction.commit();
    }

    private void hiddenFragments(){
        if (mFragments != null) {
            for (Fragment fragment : mFragments) {
                FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
                fragmentTransaction.hide(fragment);
                fragmentTransaction.commit();
            }
        }
    }

    // 设置默认进来是tab 显示的页面
    private void setDefaultFragment() {
        fragmentManager = getSupportFragmentManager();
        transaction = fragmentManager.beginTransaction();
        fragMagnifier = new MagnifierFragment();
        transaction.replace(R.id.content, fragMagnifier);
        transaction.commit();
    }



    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if(fragMagnifier != null){
            fragMagnifier.onFragmentRequestPermissionResult(requestCode,permissions,grantResults);
        }
    }
}